package com.dly.blog.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.http.server.HttpServerResponse;
import com.dly.blog.content.RedisContent;
import com.dly.blog.domain.dto.ImageCaptchaDTO;
import com.dly.blog.service.ImageCaptchaService;
import com.dly.blog.utile.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Base64;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: DaiLiYu
 * @Date: 2023/7/19 14:02
 */
@Service
@AllArgsConstructor
public class ImageCaptchaServiceImpl implements ImageCaptchaService {


    private final RedisUtils redisUtils;

    /**
     * 生成验证码 并用redis缓存对象
     */
    @Override
    public ImageCaptchaDTO imageCaptcha() {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(90, 35, 4, 20);
        //生成唯一标识
        String uuid = UUID.randomUUID().toString();
        redisUtils.setCacheObject(RedisContent.CAPTCHA + uuid, lineCaptcha.getCode(), 2, TimeUnit.MINUTES);
        return new ImageCaptchaDTO(uuid, lineCaptcha.getImageBase64());
    }

}
